package LeetCode;

import java.util.Arrays;

/**
 * @author VX5
 * @Title: MJC
 * @ProjectName interview
 * @Description: TODO
 * @date ${DAT}8:37
 */
public class checkAndSet {
    private static final int NUMS = 6;

    public static int find_root(int x,int parent[]){
        int x_root = x;
        while (parent[x_root] != -1){// 找到起源的地方
            x_root = parent[x_root];
        }
        return x_root;
    }

    /**
     * 合并  返回1成功 返回0失败
     * @param x
     * @param y
     * @param parent
     * @return
     */
    public static int union_vertices(int x,int y,int[] parent){
        int x_root = find_root(x,parent);
        int y_root = find_root(y,parent);
        if (x_root == y_root){
            return 0;
        }else {
            parent[x_root] = y_root;//进行合并
            return 1;
        }
    }

    public static void main(String[] args) {
        int[] parent = new int[NUMS];
        Arrays.fill(parent,-1);
        int[][] egdg = {
                {0,1},{1,2},{2,3},{3,4},{4,5},{5,0}
        };
        for (int i = 0; i < NUMS; i++){
            int x = egdg[i][0];
            int y = egdg[i][1];
            if (union_vertices(x,y,parent) == 0){
                System.out.println("有圈");
                return;
            }
        }
        System.out.println("没有圈");
    }

}
